﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace HospitalMedicineTraceability
{
    public partial class frmGoodsCodeCheck_Single : DevComponents.DotNetBar.OfficeForm
    {
        public Dictionary<string, string> m_dictInfo = new Dictionary<string, string>(); // 回传信息(指定对码时用)
        public string m_strSupervisionCode = "";
        public frmGoodsCodeCheck_Single()
        {
            InitializeComponent();
        }
        #region  1.自定义方法
        /// <summary>
        /// 初始化
        /// </summary>
        private void _init() {
            // 控件赋初始值
            if (!m_dictInfo.ContainsKey("Code0"))
            {
                m_dictInfo.Add("Code0","");
                m_dictInfo.Add("Name0", "");
                m_dictInfo.Add("PackageSpec0", "");
                m_dictInfo.Add("Manufacturer0", "");
                m_dictInfo.Add("Unit0", "");
                m_dictInfo.Add("Code", "");
                m_dictInfo.Add("Name", "");
                m_dictInfo.Add("PackageSpec", "");
                m_dictInfo.Add("Manufacturer", "");
                m_dictInfo.Add("Unit", "");
            }
            txtCode0.Text = m_dictInfo["Code0"];
            txtName0.Text = m_dictInfo["Name0"];
            txtPackageSpec0.Text = m_dictInfo["PackageSpec0"];
            txtManufacturer0.Text = m_dictInfo["Manufacturer0"];
            txtUnit0.Text = m_dictInfo["Unit0"];
            txtCode.Text = m_dictInfo["Code"];
            txtName.Text = m_dictInfo["Name"];
            txtPackageSpec.Text = m_dictInfo["PackageSpec"];
            txtManufacturer.Text = m_dictInfo["Manufacturer"];
            txtUnit.Text = m_dictInfo["Unit"];
        }

        private void _clear()
        {
            m_dictInfo["Code0"] = "";
            m_dictInfo["Name0"] = "";
            m_dictInfo["PackageSpec0"] = "";
            m_dictInfo["Manufacturer0"] = "";
            m_dictInfo["Unit0"] = "";
            m_dictInfo["Code"] = "";
            m_dictInfo["Name"] = "";
            m_dictInfo["PackageSpec"] = "";
            m_dictInfo["Manufacturer"] = "";
            m_dictInfo["Unit"] = "";
            _init();
            txtSupervisionCode.Text = "";
            txtSupervisionCode.Focus();
        }

        private void _save() {
            if (txtCode.Text.Equals("")) {
                CommFun._messageBoxShow("请先确定His商品！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            // 清除原有数据
            string strSql = "exec P_DealGoodsCodeCheck 'Save','{0},{1},{2},{3}'";
            strSql = string.Format(strSql, txtCode0.Text, txtCode.Text, AppInfo.m_UserInfo.m_strCode, AppInfo.m_UserInfo.m_strName);
            DataTable dt = null;
            dt = AppInfo._openSql_DataTable(strSql);

            if (dt!=null && dt.Rows.Count > 0 && dt.Rows[0]["IsOK"].ToString() == "1")
            {
                CommFun._messageBoxShow("His商品对码成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                _clear();
            }
            else {
                CommFun._messageBoxShow(string.Format("His商品对码失败！{0}", dt.Rows[0]["Message"].ToString()), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 处理编码
        /// </summary>
        private void _dealCode()
        {
            try
            {
                //电监码核对
                string strSql = "";
                strSql = string.Format("exec P_DealGoodsCodeCheck 'GetCodeCheck','{0}'", m_strSupervisionCode);
                DataTable dt = AppInfo._openSql_DataTable(strSql);
                if (dt != null && dt.Rows.Count > 0)
                {
                    m_dictInfo["Code0"] = dt.Rows[0]["Code0"].ToString();
                    m_dictInfo["Name0"] = dt.Rows[0]["Name0"].ToString();
                    m_dictInfo["PackageSpec0"] = dt.Rows[0]["PackageSpec0"].ToString();
                    m_dictInfo["Manufacturer0"] = dt.Rows[0]["Manufacturer0"].ToString();
                    m_dictInfo["Unit0"] = dt.Rows[0]["Unit0"].ToString();
                    m_dictInfo["Code"] = dt.Rows[0]["Code"].ToString();
                    m_dictInfo["Name"] = dt.Rows[0]["Name"].ToString();
                    m_dictInfo["PackageSpec"] = dt.Rows[0]["PackageSpec"].ToString();
                    m_dictInfo["Manufacturer"] = dt.Rows[0]["Manufacturer"].ToString();
                    m_dictInfo["Unit"] = dt.Rows[0]["Unit"].ToString();
                    _init();
                    if (string.IsNullOrEmpty(m_dictInfo["Code"]))
                    {
                        btnQuery_Click(null, null);
                    }
                }
            }
            catch { }
            txtSupervisionCode.Text = "";
            txtSupervisionCode.Focus();
        }
        #endregion

        #region 2.系统事件方法
        private void frmEnterpriseCodeCheck_Load(object sender, EventArgs e)
        {
            _init();
            txtSupervisionCode.Focus();
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            _save();
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void btnQuery_Click(object sender, EventArgs e)
        {
            frmQuerySelect frm = new frmQuerySelect();
            frm.m_nKind = 10;
            frm.m_strInfo = string.IsNullOrEmpty(m_dictInfo["Name0"])? m_dictInfo["Name"]: m_dictInfo["Name0"];
            frm.m_strWhere = " ";
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtCode.Text = frm.m_dictSelected["Code"];
                txtName.Text = frm.m_dictSelected["Name"];
                txtPackageSpec.Text = frm.m_dictSelected["PackageSpec"];
                txtManufacturer.Text = frm.m_dictSelected["Manufacturer"];
                txtUnit.Text = frm.m_dictSelected["Unit"];
            }
        }

        private void txtSupervisionCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtSupervisionCode.Text.Trim().Length != 20)
                {
                    CommFun._messageBoxShow("电监编码长度不合法,应为20位！");
                    txtSupervisionCode.Text = "";
                    txtSupervisionCode.Focus();
                    return;
                }
                m_strSupervisionCode = txtSupervisionCode.Text.Trim();
                _dealCode();
            }
        }

        private void frmGoodsCodeCheck_Single_Shown(object sender, EventArgs e)
        {
            txtSupervisionCode.Focus();
        }
    }
}
